<template>
    <div class="discussion-block">
        <h2>🗣️ 讨论区</h2>
        <!-- 分区选择卡片 -->
        <div class="zone-grid">
            <div v-for="zone in zones" :key="zone.key" class="zone-card" :class="{ active: zone.key === selectedZone }"
                @click="changeZone(zone.key)">
                <!-- <img :src="zone.image" /> -->
                <h3>{{ zone.title }}</h3>
                <p>{{ zone.desc }}</p>
                <button>查看留言</button>
            </div>
        </div>

        <!-- 留言 + 评论区域 -->
        <div class="message-panel" v-if="selectedZoneObj">
            <h3>{{ selectedZoneObj.title }}</h3>

            <div v-if="!isLoggedIn" class="login-tip">
                <p>请 <a href="/login">登录</a> 后发表评论</p>
            </div>

            <div v-else class="message-section">
                <textarea v-model="newmessage" placeholder="写下你的评论..." rows="3" />
                <button @click="submitmessage">发表</button>
            </div>

            <div class="messages-list">
                <div v-for="message in messages" :key="message.id" class="message">
                    <div class="meta">
                        <strong>{{ message.username }}</strong>
                        <span>{{ message.created_at }}</span>
                    </div>
                    <p>{{ message.content }}</p>
                    <div class="like-bar">
                        <button @click="likemessage(message.id)">👍 {{ message.likes }}</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script setup>
import { ref, onMounted, watch, computed } from 'vue'
import axios from '@/utils/axios.js'
import { checkLogin } from '@/utils/auth'

const isLoggedIn = ref(false)
const newmessage = ref('')
const messages = ref([])
const zones = ref([])
const selectedZone = ref('')

// 当前分区对象
const selectedZoneObj = computed(() =>
    zones.value.find(z => z.key === selectedZone.value)
)

// 加载分区
async function fetchZones() {
    const res = await axios.get('/zones')
    zones.value = res.data.data || []
    // 默认选择第一个
    if (zones.value.length > 0) selectedZone.value = zones.value[0].key
}
onMounted(fetchZones)

// 切换分区
function changeZone(key) {
    selectedZone.value = key
}

// 留言
async function fetchmessages() {
    if (!selectedZone.value) return
    const { data } = await axios.get('/messages', { params: { area: selectedZone.value } })
    messages.value = Array.isArray(data) ? data : []
}
watch(selectedZone, fetchmessages)

// 登录检测
onMounted(async () => {
    isLoggedIn.value = await checkLogin()
})

// 发表评论
async function submitmessage() {
    if (!newmessage.value.trim()) return alert('评论不能为空')
    await axios.post('/messages', {
        content: newmessage.value,
        area: selectedZone.value
    }, { withCredentials: true })
    newmessage.value = ''
    fetchmessages()
}

// 点赞
async function likemessage(id) {
    await axios.post(`/messages/${id}/like`, {}, { withCredentials: true })
    fetchmessages()
}
</script>

<style scoped>
.discussion-block {
    max-width: 900px;
    margin: 0 auto;
    padding: 40px 20px;
    color: white;
}

.zone-grid {
    display: flex;
    flex-wrap: wrap;
    justify-content: center;
    gap: 24px;
    margin-bottom: 40px;
}

.zone-card {
    background: rgba(255, 255, 255, 0.1);
    padding: 20px;
    width: 200px;
    border-radius: 12px;
    text-align: center;
    cursor: pointer;
    transition: transform 0.2s;
}

.zone-card:hover {
    transform: scale(1.05);
    background: rgba(255, 255, 255, 0.2);
}

.zone-card img {
    width: 80px;
    height: 80px;
    object-fit: contain;
    margin-bottom: 10px;
}

.zone-card h3 {
    margin: 10px 0 5px;
    font-size: 18px;
    color: #fff;
}

.zone-card p {
    font-size: 13px;
    color: #ccc;
}

.zone-card button {
    margin-top: 10px;
    padding: 6px 12px;
    background: #42b983;
    border: none;
    color: white;
    border-radius: 6px;
    cursor: pointer;
}

.message-panel {
    background: #f5f5f5;
    padding: 20px;
    border-radius: 12px;
    color: #333;
}

.message-section textarea {
    width: 100%;
    padding: 10px;
    font-size: 14px;
    border-radius: 6px;
    border: 1px solid #ccc;
}

.message-section button {
    margin-top: 8px;
    padding: 6px 16px;
    background-color: #42b983;
    color: white;
    border: none;
    cursor: pointer;
    border-radius: 6px;
}

.messages-list {
    margin-top: 20px;
}

.message {
    background: #fff;
    padding: 14px;
    margin-bottom: 12px;
    border-radius: 8px;
}

.meta {
    display: flex;
    justify-content: space-between;
    font-size: 13px;
    color: #555;
}

.like-bar {
    text-align: right;
    margin-top: 6px;
}
</style>
